// 导出页面为PDF格式
import html2Canvas from 'html2canvas'
import JsPDF from 'jspdf'
export default {
    install (Vue, options) {
        // targetDom为dom节点 isDownLoad scrollWidth 页面宽度 title 下载文件的标题
        Vue.prototype.getPdf = (targetDom, isDownLoad, scrollWidth, title) => {
            return new Promise((resolve, reject) => {
                html2Canvas(document.querySelector(targetDom), {
                    allowTaint: true,
                    dpi: 2,//导出pdf清晰度
                    scale: 2, // 提升画面质量，但是会增加文件大小
                    width: scrollWidth || document.querySelector(targetDom).scrollWidth
                }).then((canvas) => {
                    let contentWidth = canvas.width / 2;
                    let contentHeight = canvas.height / 2
                    let pageHeight = contentWidth / 592.28 * 841.89
                    let leftHeight = contentHeight
                    let position = 0
                    let imgWidth = 595.28
                    let imgHeight = 592.28 / contentWidth * contentHeight
                    // let pageData = canvas.toDataURL('image/jpeg', 1.0)
                    // let PDF = new JsPDF('', 'pt', 'a4');
                    const PDF = new JsPDF('', 'pt', [contentWidth, contentHeight]);
                    const pageData = canvas.toDataURL('image/jpeg', 1.0);
                    PDF.addImage(pageData, 'JPEG', 0, 0, contentWidth, contentHeight);
                    if (isDownLoad) {
                        PDF.save(title + '.pdf');
                    } else {
                        const base64 = PDF.output('bloburl');
                        window.open(base64)
                    }
                    PDF.save(title + '.pdf');
                    resolve(123)
                }
                )
            })

        }
    }
}